<?php
	App::import("Controller","Import.AbstractImport");
	class ListingImportController extends AbstractImportController
	{
		var $name = "ListingImport";
		var $modelAlias = "ListingImport";
		var $components = array("Email");
		/**
		 * 
		 * Enter description here ...
		 * @var ListingImport
		 */
		var $ListingImport;
		function admin_import()
		{
			$this->getImportModel()->setImporFieldProperty();
			$this->pageTitle = __("Import");
			parent::import();
			$this->render("import");
		}
		
		function admin_parse($fileId)
		{
			$this->getImportModel()->setImporFieldProperty();
			$this->pageTitle = __("Import");
			$fileInfo = $this->getImportModel()->getFileModel()->getFileInfo($fileId);
			$categoryId = $fileInfo['other_information']['importData']['list_category_id'];
			$this->ListingImport->setImportCategory($categoryId);
			parent::parse($fileId);
			$this->render("parse");
		}
		
		function admin_startImporting($fileId)
		{
			parent::startImporting($fileId);
			$this->render("startImporting");
		}
		function admin_runBatch($fileId,$batchSize =100)
		{
			parent::runBatch($fileId,$batchSize);
			$this->_stop();
		}
		protected function beforeBatchStart($fileId,$fileInfo)
		{
			$categoryId = $fileInfo['other_information']['importData']['list_category_id'];
			$this->ListingImport->setImportCategory($categoryId);
		}
		function import()
		{
			$this->cakeError("error404");
		}
		
		function parse()
		{
			$this->cakeError("error404");
		}
		
		function startImporting()
		{
			$this->cakeError("error404");
		}
		
		function deleteImportFile()
		{
			$this->cakeError("error404");
		}
		function admin_deleteImportFile($id)
		{
			parent::deleteImportFile($id);
		}
		function runBatch()
		{
			$this->cakeError("error404");
		}
		protected function saveToModel($data,$fileInfo)
		{
			if($data)
			{
				$data['user_id'] = $fileInfo['other_information']['importData']['user_id'];
				if($fileInfo['other_information']['importData']['listing_status'] == Listing::PUBLISH)
				{
					Configure::write("ListingConfig.enable_moderation",false);
					$data['status'] = Listing::PUBLISH;
				}else
				{
					Configure::write("ListingConfig.enable_moderation",true);
					$data['status'] = Listing::UNPUBLISH;
				}
			}
			return parent::saveToModel($data,$fileInfo);
		}
		protected function afterRowInsert($fileInfo,$saved)
		{
			if($saved)
			{
				$ListingModel = $this->getImportModel()->getImporterModel();
				$ListingModel->recursive = -1;
				$ListingModel->findById($ListingModel->id);
			}
			parent::afterBatchFinish($fileInfo,$saved);
			
		}
	}
?>